package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.List;

import com.xcity.db.entity.Guild;
import com.xcity.db.entity.GuildApply;
import com.xcity.game.guild.GuildService;

import naga.x.App;
import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class GuildListResponse extends Response {
	
	private List<GuildInfo> list;
	
	public static class GuildInfo{
		public long id;
		public int level;
		public int type1;
		public int type2;
		public int count;
		public int maxCount;
		public String name;
		public int apply = 0;
		public GuildInfo(Guild guild, int lvl){
			this.id = guild.getId();
			this.level = lvl;
			this.type1 = (guild.getFlag() >> 8) & 0xFF;
			this.type2 = guild.getFlag() & 0xFF;
			this.count = guild.getMemCount();
			this.maxCount = GuildService.MAX_MEMBER_COUNT;//TODO
			this.name = guild.getName();
		}
		
		public GuildInfo(Guild guild, int lvl, int state){
			this(guild, lvl);
			this.id = guild.getId();
			this.level = lvl;
			this.type1 = (guild.getFlag() >> 8) & 0xFF;
			this.type2 = guild.getFlag() & 0xFF;
			this.count = guild.getMemCount();
			this.maxCount = GuildService.MAX_MEMBER_COUNT;//TODO
			this.name = guild.getName();
			this.apply = state;
		}
	}
	public GuildListResponse(int serial, List<Guild> guildList, List<GuildApply> applyList) {
		super(serial);
		GuildService gs = App.getApp().getServiceManager().get(GuildService.class);
		list = new ArrayList<>();
		int applyCount = applyList.size();
		for(int i = 0, size = guildList.size(); i < size; i++){
			Guild guild = guildList.get(i);
			int lvl = gs.exp2level(guild.getExp());
			int state = 0;
			for(int j = 0; j < applyCount; j++){
				if(applyList.get(j).getGuildId() == guild.getId()){
					state = 1;
					break;
				}
			}
			GuildInfo info = new GuildInfo(guild, lvl, state);
			list.add(info);
		}
	}
	public List<GuildInfo> getList() {
		return list;
	}
	public void setList(List<GuildInfo> list) {
		this.list = list;
	}
}
